package com.ssmTest.service.impl;

import com.ssmTest.service.DateTimeService;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Service("dateTimeService")
public class DateTimeServiceImpl implements DateTimeService {
    public String getTime() {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//日期格式
        return dateFormat.format(now);
    }

    public String getTime(String s) {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(s);//日期格式
        return dateFormat.format(now);
    }

    public String addday(String s, int n) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        Date date = sdf.parse(s);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) + n);// 调整加n天
        System.out.println(sdf.format(cal.getTime()));
        return sdf.format(cal.getTime());
    }
}
